package net.jgrm.product.controller;

import net.jgrm.product.entity.Product;
import net.jgrm.product.service.IProductService;
import net.jgrm.product.service.IProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 缓存测试控制器（最终优化版）
 * <p>
 * 测试简化后的架构：
 * 1. ProductService: product-info 缓存（商品基本信息）
 * 2. ProductStockService: product-stock 缓存（库存信息）
 * 3. Controller组合两个缓存的数据
 */
@RestController
@RequestMapping("/cache-test")
public class CacheTestController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductStockService productStockService;

    /**
     * 测试 product-info 缓存（重点测试）
     * 第一次请求会查询数据库，后续请求应该命中缓存
     */
    @GetMapping("/product-info/{id}")
    public Product testProductInfoCache(@PathVariable Long id) {
        long startTime = System.currentTimeMillis();
        Product result = productService.getProductById(id);
        long endTime = System.currentTimeMillis();

        System.out.println("=== product-info 缓存测试 ===");
        System.out.println("商品ID: " + id);
        System.out.println("查询耗时: " + (endTime - startTime) + "ms");
        System.out.println("缓存类型: product-info");
        System.out.println("==========================");

        return result;
    }

    /**
     * 测试 product-stock 缓存
     */
    @GetMapping("/product-stock/{id}")
    public Integer testProductStockCache(@PathVariable Long id) {
        long startTime = System.currentTimeMillis();
        Integer result = productStockService.getStock(id);
        long endTime = System.currentTimeMillis();

        System.out.println("=== product-stock 缓存测试 ===");
        System.out.println("商品ID: " + id);
        System.out.println("查询耗时: " + (endTime - startTime) + "ms");
        System.out.println("库存数量: " + result);
        System.out.println("缓存类型: product-stock");
        System.out.println("===========================");

        return result;
    }

    /**
     * 测试组合服务（同时使用两个缓存）
     */
    @GetMapping("/complete-info/{id}")
    public Product testCompleteInfo(@PathVariable Long id) {
        long startTime = System.currentTimeMillis();

        // 1. 获取商品基本信息（命中 product-info 缓存）
        Product product = productService.getProductById(id);

        // 2. 获取实时库存（命中 product-stock 缓存）
        Integer stock = productStockService.getStock(id);
        product.setStock(stock);

        long endTime = System.currentTimeMillis();

        System.out.println("=== 组合服务测试 ===");
        System.out.println("商品ID: " + id);
        System.out.println("查询耗时: " + (endTime - startTime) + "ms");
        System.out.println("商品名称: " + (product != null ? product.getProductName() : "null"));
        System.out.println("库存数量: " + (product != null ? product.getStock() : "null"));
        System.out.println("缓存策略: product-info + product-stock");
        System.out.println("===================");

        return product;
    }

    /**
     * 清理 product-info 缓存
     */
    @GetMapping("/clear-info-cache/{id}")
    public String clearInfoCache(@PathVariable Long id) {
        productService.invalidateProductInfoCache(id);
        return "已清理商品 " + id + " 的 product-info 缓存";
    }

    /**
     * 清理 product-stock 缓存
     */
    @GetMapping("/clear-stock-cache/{id}")
    public String clearStockCache(@PathVariable Long id) {
        productStockService.refreshStockCache(id);
        return "已清理商品 " + id + " 的 product-stock 缓存";
    }
}